package org.dllwh.mysql.utils;


import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.jasypt.iv.NoIvGenerator;
import org.jasypt.iv.RandomIvGenerator;
import org.jasypt.salt.RandomSaltGenerator;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName JasyptUtil
 * @Description Jasypt加密、解密工具类
 * @Date 2025-09-18 9:13
 * @版本 V 1.0.1
 * @since JDK 17
 */
public class JasyptEncryptionUtil {
    /**
     * 加密算法
     */
    public static final String PBE_ALGORITHMS_MD5_DES = "PBEWITHMD5ANDDES";
    public static final String PBE_ALGORITHMS_MD5_TRIPLEDES = "PBEWITHMD5ANDTRIPLEDES";
    public static final String PBE_ALGORITHMS_SHA1_DESEDE = "PBEWITHSHA1ANDDESEDE";
    public static final String PBE_ALGORITHMS_SHA1_RC2_40 = "PBEWITHSHA1ANDRC2_40";

    public static void main(String[] args) {
        String password = "1234567890";
        System.out.println("解密：" + decrypt("NLUne1EgjFai1mmZS+FoReb3VP4q4j1H8ZBnpMdUnJc=", password));
    }

    /**
     * Jasypt 使用默认算法（PBEWithMD5AndDES）加密字符串
     *
     * @param encryptedStr 加密字符串
     * @param password     加密密码
     * @return 加密后的字符串
     */
    public static String encrypt(String encryptedStr, String password) {
        return encrypt(encryptedStr, PBE_ALGORITHMS_MD5_DES, password);
    }

    /**
     * Jasypt 使用指定算法和密码加密字符串
     *
     * @param encryptedStr 加密字符串
     * @param algorithm    加密算法
     * @param password     加密密码
     * @return 加密后的字符串
     */
    public static String encrypt(String encryptedStr, String algorithm, String password) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        config.setAlgorithm(algorithm);
        config.setPassword(password);
        // 对应 jasypt-spring-boot-starter 中的配置 ivGenerator 缺省值
        config.setIvGenerator(new RandomIvGenerator());
        // 对应 jasypt-spring-boot-starter 中的配置 saltGenerator 缺省值
        config.setSaltGenerator(new RandomSaltGenerator());
        encryptor.setConfig(config);
        return encryptor.encrypt(encryptedStr);
    }

    /**
     * Jasypt 使用默认算法（PBEWithMD5AndDES）解密字符串
     *
     * @param decryptStr 解密字符串
     * @param password   盐值
     */
    public static String decrypt(String decryptStr, String password) {
        return decrypt(decryptStr, PBE_ALGORITHMS_MD5_DES, password);
    }

    /**
     * Jasypt 解密
     *
     * @param decryptStr 解密字符串
     * @param algorithm  指定解密算法：解密算法要与加密算法一一对应
     * @param password   盐值
     */
    public static String decrypt(String decryptStr, String algorithm, String password) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        config.setAlgorithm(algorithm);
        config.setPassword(password);
        // 对应 jasypt-spring-boot-starter 中的配置 ivGenerator 缺省值
        config.setIvGenerator(new RandomIvGenerator());
        // 对应 jasypt-spring-boot-starter 中的配置 saltGenerator 缺省值
        encryptor.setConfig(config);
        return encryptor.decrypt(decryptStr);
    }
}
